package com.kundi.dooradmin.service;

import com.kundi.dooradmin.entity.IndustryInformation;
import com.kundi.dooradmin.mapper.IndustryInformationMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author 赵恒
 * Date 2020/6/24
 */
@Service
public class IndustryInformationService {
    @Resource
    IndustryInformationMapper industryInformationMapper;
    public List<IndustryInformation> getAll() {
        return industryInformationMapper.getAll();
    }

    public IndustryInformation getIndustryById(Integer id) {
        return industryInformationMapper.getIndustryById(id);
    }

    public Integer insert(IndustryInformation industryInformation) {
        return industryInformationMapper.insertSelective(industryInformation);
    }

    public Integer update(IndustryInformation industryInformation) {
        return industryInformationMapper.updateByPrimaryKeySelective(industryInformation);
    }


    public Integer delete(Integer id) {
        IndustryInformation industryInformation = new IndustryInformation();
        industryInformation.setId(id);
        return industryInformationMapper.delete(industryInformation);
    }

    public IndustryInformation getIndustryByIdWithImage(Integer id) {
        return industryInformationMapper.getIndustryByIdWithImage(id);
    }

    public IndustryInformation getNewInformation() {
        return industryInformationMapper.getAll().get(0);
    }

    /**
     * 根据ID获取前一个对象
     * @param id
     * @return
     */
    public IndustryInformation getPreInformation(Integer id) {
        List<IndustryInformation> informations = getAll();
        IndustryInformation information = new IndustryInformation();
        for(int i=0; i<informations.size(); i++) {
            if(informations.get(i).getId() == id) {
                if(i == 0) {
                    information = informations.get(i);
                } else {
                    information = informations.get(i-1);
                }
                break;
            }
        }
        return information;
    }

    public IndustryInformation getNextInformation(Integer id) {
        List<IndustryInformation> informations = getAll();
        IndustryInformation information = new IndustryInformation();
        for(int i=0; i<informations.size();i++) {
            if(informations.get(i).getId() == id) {
                if(i == informations.size() - 1) {
                    information = informations.get(i);
                } else {
                    information = informations.get(i +1);
                }
                break;
            }
        }
        return information;
    }
}
